﻿using System;
using System.Web.UI;
using System.Net.Mail;
using System.Text;

#region HTML TO PDF
//For converting HTML TO PDF- START
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.xml;
using System.IO;
//For converting HTML TO PDF- END
#endregion

namespace WebApplication2
{
    public partial class Formulario_web1 : System.Web.UI.Page
    {
        #region Variables Base de Datos - Dataset
        //Variables Base de Datos dataset
        private WebApplication2.BD BD;
        private WebApplication2.BDTableAdapters.PreguntaTableAdapter preguntaTableAdapter;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
    
            inicio();
            cargaPreguntas();
        }

        #region Inicializador
        protected void inicio()
        {
            this.BD = new WebApplication2.BD();
            this.preguntaTableAdapter = new WebApplication2.BDTableAdapters.PreguntaTableAdapter();

            #region Dataset BD
            ((System.ComponentModel.ISupportInitialize)(this.BD)).BeginInit();
            //
            // dataSet BD
            // 
            this.BD.DataSetName = "BD";

            #region TablaAdapter reguntaTableAdapte
            // 
            // preguntaTableAdapter de BD
            // 
            this.preguntaTableAdapter.ClearBeforeFill = true;
            #endregion

            this.BD.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            ((System.ComponentModel.ISupportInitialize)(this.BD)).EndInit();
            #endregion


        }
        protected void cargaPreguntas()
        {
            preguntaTableAdapter.Fill(BD.Pregunta);
            txtP1.Text = BD.Pregunta[0].valorPreg;
            txtP2.Text = BD.Pregunta[1].valorPreg;
            txtP3.Text = BD.Pregunta[2].valorPreg;
            txtP4.Text = BD.Pregunta[3].valorPreg;
            txtP5.Text = BD.Pregunta[4].valorPreg;
            txtP6.Text = BD.Pregunta[5].valorPreg;
            txtP7.Text = BD.Pregunta[6].valorPreg;
            txtP8.Text = BD.Pregunta[7].valorPreg;
            txtP9.Text = BD.Pregunta[8].valorPreg;
            txtP10.Text = BD.Pregunta[9].valorPreg;
            txtP11.Text = BD.Pregunta[10].valorPreg;
            txtP12.Text = BD.Pregunta[11].valorPreg;
            txtP13.Text = BD.Pregunta[12].valorPreg;
            txtP14.Text = BD.Pregunta[13].valorPreg;
            txtP15.Text = BD.Pregunta[14].valorPreg;
            txtP16.Text = BD.Pregunta[15].valorPreg;
            txtP17.Text = BD.Pregunta[16].valorPreg;
            txtP18.Text = BD.Pregunta[17].valorPreg;
            txtP19.Text = BD.Pregunta[18].valorPreg;
            txtP20.Text = BD.Pregunta[19].valorPreg;
            txtP21.Text = BD.Pregunta[20].valorPreg;
            txtP22.Text = BD.Pregunta[21].valorPreg;
            txtP23.Text = BD.Pregunta[22].valorPreg;
            txtP24.Text = BD.Pregunta[23].valorPreg;
            txtP25.Text = BD.Pregunta[24].valorPreg;
        }
        #endregion

        protected void btEnviar_Click(object sender, EventArgs e)
        {
            
            ocultar();
            MostrarDatosAlumno();
            htmlpdf();
            mostrarOculto();
        }

        #region EnvioDeArchivo
        protected void pageEnvio() { 
                   
           
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Page page = new Page();

            //HtmlForm form = new HtmlForm();
            this.EnableViewState = false;
            // Deshabilitar la validación de eventos, sólo asp.net 2
            page.EnableEventValidation = false;
            // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.
            page.DesignerInitialize();
            page.Controls.Add(this.Form); 
            Session.Add("PAGE", page);
        }
        protected void htmlpdf()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Page page = new Page();

            //HtmlForm form = new HtmlForm();
            this.EnableViewState = false;
            // Deshabilitar la validación de eventos, sólo asp.net 2
            page.EnableEventValidation = false;
            // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.
            page.DesignerInitialize();
            page.Controls.Add(this.Form);
           
            //form.Controls.Add(txtApellido);
            page.RenderControl(htw);
            this.EnableViewState = true;
            string strs = " ";
            Document document = new Document();
            int estado = 1;
            try
            {
                var output = new FileStream(Server.MapPath("MiExamenDiplomatura - " + lb2NroDoc.Text + ".pdf"), FileMode.Create);
                PdfWriter.GetInstance(document, output);

                document.Open();

                StringReader str = new StringReader(sw.ToString());

                HTMLWorker htmlworker = new HTMLWorker(document);

                htmlworker.Parse(str);

                document.Close();
                this.RegisterRequiresControlState(this.Form);
                strs = Server.MapPath("MiExamenDiplomatura - " + lb2NroDoc.Text + ".pdf");
                email(strs);

            }
            catch (IOException)
            {
                estado = 0;
                lbControl.Visible = true;
                lbControl.Text = "Pruebe anviar en 2 minutos. Servidor Colapsado.";
                Response.Buffer = true;
                Response.Write("<script>alert('Envielo de vuelta - Servidor Ocupado - Vuelva para Atrás');</script>");
            }
            if (estado == 1)
            {
                Session.Add("url", strs);
                Response.Redirect("FormularioWeb2.aspx");
            }
        }
        protected void email()
        {
            //preguntaTableAdapter.Fill(BD.Pregunta);
            //txtP1.Text = BD.Pregunta[0].valorPreg;
            //Creación del mail
            MailMessage correo = new MailMessage();
            correo.From = new System.Net.Mail.MailAddress("antonioedgardo@gmail.com");
            correo.To.Add("antonioedgardo@gmail.com");
            correo.Subject = "Examen de Diplomatura Web";
            correo.Body = "Examen";
            //correo.Attachments.Add(null);
            if (FileUpload1.HasFile)
            {
                string ArchvioSubir = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
                Attachment attachmentFile = new Attachment(FileUpload1.PostedFile.InputStream, ArchvioSubir);
                correo.Attachments.Add(attachmentFile);
            }

            correo.IsBodyHtml = true;
            correo.Priority = System.Net.Mail.MailPriority.Normal;

            //configuración de envio
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("antonioedgardo@gmail.com", "Sigma123456");

            //Envio de mail
            try
            {
                smtp.Send(correo);
                lbControl.Text = "Mensaje enviado satisfactoriamente";
            }
            catch (Exception ex)
            {
                lbControl.Text = "ERROR: " + ex.Message;
            }

        }
        protected void email(string strs)
        {
            //Creación del mail
            MailMessage correo = new MailMessage();
            correo.From = new System.Net.Mail.MailAddress("antonioedgardo@gmail.com");
            correo.To.Add("antonioedgardo@gmail.com");
            correo.Subject = "Examen de Diplomatura Web";
            correo.Body = "Examen " + DateTime.Now.ToString();
            if (strs.Length > 0)
            {
                string ArchvioSubir = System.IO.Path.GetFileName(strs);
                Attachment attachmentFile = new Attachment(strs);
                correo.Attachments.Add(attachmentFile);
            }
            correo.IsBodyHtml = true;
            correo.Priority = System.Net.Mail.MailPriority.Normal;

            //configuración de envio
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("antonioedgardo@gmail.com", "Sigma123456");

            //Envio de mail
            try
            {
                smtp.Send(correo);
                lbControl.Text = "Mensaje enviado satisfactoriamente";
            }
            catch (Exception ex)
            {
                lbControl.Text = "ERROR: " + ex.Message;
            }
        }
        

        #endregion

        #region Micelanios

        protected void ocultar()
        {
            RadioButtonList1.Visible = false;
            RadioButtonList2.Visible = false;
            RadioButtonList3.Visible = false;
            RadioButtonList4.Visible = false;
            RadioButtonList5.Visible = false;
            RadioButtonList6.Visible = false;
            RadioButtonList7.Visible = false;
            RadioButtonList8.Visible = false;
            RadioButtonList9.Visible = false;
            RadioButtonList10.Visible = false;
            RadioButtonList11.Visible = false;
            RadioButtonList12.Visible = false;
            RadioButtonList13.Visible = false;
            RadioButtonList14.Visible = false;
            RadioButtonList15.Visible = false;
            RadioButtonList16.Visible = false;
            RadioButtonList17.Visible = false;
            RadioButtonList18.Visible = false;
            RadioButtonList19.Visible = false;
            RadioButtonList20.Visible = false;
            RadioButtonList21.Visible = false;
            RadioButtonList22.Visible = false;
            RadioButtonList23.Visible = false;
            RadioButtonList24.Visible = false;
            RadioButtonList25.Visible = false;
            lbControl.Visible = false;
            FileUpload1.Visible = false;

        }

        protected void MostrarDatosAlumno()
        {
            lb2Apellido.Text = txtApellido.Text;
            lb2Nombre.Text = txtNombre.Text;
            lb2NroDoc.Text = txtNroDoc.Text;
            lb2TipoDoc.Text = txtTipoDoc.Text;
        }

        protected void mostrarOculto()
        {
            RadioButtonList1.Visible = true;
            RadioButtonList2.Visible = true;
            RadioButtonList3.Visible = true;
            RadioButtonList4.Visible = true;
            RadioButtonList5.Visible = true;
            RadioButtonList6.Visible = true;
            RadioButtonList7.Visible = true;
            RadioButtonList8.Visible = true;
            RadioButtonList9.Visible = true;
            RadioButtonList10.Visible = true;
            RadioButtonList11.Visible = true;
            RadioButtonList12.Visible = true;
            RadioButtonList13.Visible = true;
            RadioButtonList14.Visible = true;
            RadioButtonList15.Visible = true;
            RadioButtonList16.Visible = true;
            RadioButtonList17.Visible = true;
            RadioButtonList18.Visible = true;
            RadioButtonList19.Visible = true;
            RadioButtonList20.Visible = true;
            RadioButtonList21.Visible = true;
            RadioButtonList22.Visible = true;
            RadioButtonList23.Visible = true;
            RadioButtonList24.Visible = true;
            RadioButtonList25.Visible = true;
            lbControl.Visible = false;
            FileUpload1.Visible = false;

        }

        #region EventosSeleccionar
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            R1.Text = this.RadioButtonList1.SelectedItem.Text;
        }
        protected void RadioButtonList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            R2.Text = this.RadioButtonList2.SelectedItem.Text;
        }
        protected void RadioButtonList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            R3.Text = this.RadioButtonList3.SelectedItem.Text;
        }
        protected void RadioButtonList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            R4.Text = this.RadioButtonList4.SelectedItem.Text;
        }
        protected void RadioButtonList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            R5.Text = this.RadioButtonList5.SelectedItem.Text;
        }
        protected void RadioButtonList6_SelectedIndexChanged(object sender, EventArgs e)
        {
            R6.Text = this.RadioButtonList6.SelectedItem.Text;
        }
        protected void RadioButtonList7_SelectedIndexChanged(object sender, EventArgs e)
        {
            R7.Text = this.RadioButtonList7.SelectedItem.Text;
        }
        protected void RadioButtonList8_SelectedIndexChanged(object sender, EventArgs e)
        {
            R8.Text = this.RadioButtonList8.SelectedItem.Text;
        }
        protected void RadioButtonList9_SelectedIndexChanged(object sender, EventArgs e)
        {
            R9.Text = this.RadioButtonList9.SelectedItem.Text;
        }
        protected void RadioButtonList10_SelectedIndexChanged(object sender, EventArgs e)
        {
            R10.Text = this.RadioButtonList10.SelectedItem.Text;
        }
        protected void RadioButtonList11_SelectedIndexChanged(object sender, EventArgs e)
        {
            R11.Text = this.RadioButtonList11.SelectedItem.Text;
        }
        protected void RadioButtonList12_SelectedIndexChanged(object sender, EventArgs e)
        {
            R12.Text = this.RadioButtonList12.SelectedItem.Text;
        }
        protected void RadioButtonList13_SelectedIndexChanged(object sender, EventArgs e)
        {
            R13.Text = this.RadioButtonList13.SelectedItem.Text;
        }
        protected void RadioButtonList14_SelectedIndexChanged(object sender, EventArgs e)
        {
            R14.Text = this.RadioButtonList14.SelectedItem.Text;
        }
        protected void RadioButtonList15_SelectedIndexChanged(object sender, EventArgs e)
        {
            R15.Text = this.RadioButtonList15.SelectedItem.Text;
        }
        protected void RadioButtonList16_SelectedIndexChanged(object sender, EventArgs e)
        {
            R16.Text = this.RadioButtonList16.SelectedItem.Text;
        }
        protected void RadioButtonList17_SelectedIndexChanged(object sender, EventArgs e)
        {
            R17.Text = this.RadioButtonList17.SelectedItem.Text;
        }
        protected void RadioButtonList18_SelectedIndexChanged(object sender, EventArgs e)
        {
            R18.Text = this.RadioButtonList18.SelectedItem.Text;
        }
        protected void RadioButtonList19_SelectedIndexChanged(object sender, EventArgs e)
        {
            R19.Text = this.RadioButtonList19.SelectedItem.Text;
        }
        protected void RadioButtonList20_SelectedIndexChanged(object sender, EventArgs e)
        {
            R20.Text = this.RadioButtonList20.SelectedItem.Text;
        }
        protected void RadioButtonList21_SelectedIndexChanged(object sender, EventArgs e)
        {
            R21.Text = this.RadioButtonList21.SelectedItem.Text;
        }
        protected void RadioButtonList22_SelectedIndexChanged(object sender, EventArgs e)
        {
            R22.Text = this.RadioButtonList22.SelectedItem.Text;
        }
        protected void RadioButtonList23_SelectedIndexChanged(object sender, EventArgs e)
        {
            R23.Text = this.RadioButtonList23.SelectedItem.Text;
        }
        protected void RadioButtonList24_SelectedIndexChanged(object sender, EventArgs e)
        {
            R24.Text = this.RadioButtonList24.SelectedItem.Text;
        }
        protected void RadioButtonList25_SelectedIndexChanged(object sender, EventArgs e)
        {
            R25.Text = this.RadioButtonList25.SelectedItem.Text;
        }
        #endregion

        #endregion

        #region NoUsados
        protected void Button1_Click(object sender, EventArgs e)
        {
            ocultar();
            MostrarDatosAlumno();
            htmlapdf2();
        }
        protected void htmlapdf2()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Page page = new Page();

            //HtmlForm form = new HtmlForm();
            this.EnableViewState = false;
            // Deshabilitar la validación de eventos, sólo asp.net 2
            page.EnableEventValidation = false;
            // Realiza las inicializaciones de la instancia de la clase Page que requieran los diseñadores RAD.
            page.DesignerInitialize();
            page.Controls.Add(this.Form);
            //form.Controls.Add(txtApellido);
            page.RenderControl(htw);

            Document document = new Document();

            PdfWriter.GetInstance(document, Response.OutputStream);

            document.Open();

            StringReader str = new StringReader(sw.ToString());

            HTMLWorker htmlworker = new HTMLWorker(document);

            htmlworker.Parse(str);

            document.Close();

            Response.Buffer = true;

            Response.Write(document);

            Response.End();

        } //v.1
        protected void boton123_Click(object sender, EventArgs e)
        {

            ocultar();
            MostrarDatosAlumno();
            htmlpdf();
        }
        protected void ShowPdf(string strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader
            ("Content-Disposition", "attachment; filename=" + strS);
            Response.TransmitFile(strS);
            Response.TransmitFile(strS);
            Response.End();
        }
        #endregion

        protected void btSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("FormularioWeb4.aspx");
        }


    }
}



